﻿using MyHospital.BLL;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyHospital.web
{
    public partial class ManageTimePart : System.Web.UI.Page
    {
        Time time = new Time();
        protected void Page_Load(object sender, EventArgs e)
        {
            lblMassage.Text = "";
            lblMassage1.Text = "";
            //Calendar.SelectedDate = "2022-6-13";
            LableInsert.Visible = false;
            ChkTime.Visible = false;
            InsertSubmit.Visible = false;
            ddTimeInser1.Visible = false;
            ddTimeInser2.Visible = false;
            ddTimeInser3.Visible = false;
            ddTimeInser4.Visible = false;
            ddTimeInser5.Visible = false;
            ddTimeInser6.Visible = false;
            LableUpdate.Visible = false;
            UpdateSubmit.Visible = false;
        }
        protected void updateDisease_Click(object sender, EventArgs e)
        {
            Session.Remove("insert");
            int i = gvDoctor.SelectedIndex;//获得所选择的医生下标
            string doctorId = gvDoctor.Rows[Convert.ToInt16(i)].Cells[0].Text;//得到医生ID
            if (i == -1)
                lblMassage1.Text = "请选择医生";
            else
            {
                if (Calendar.SelectedDate.ToString("yyyy-MM-dd") == "0001-01-01")
                {
                    lblMassage1.Text = "请选择日期";
                }
                else if (time.isExitDate(Calendar.SelectedDate.ToString("yyyy-MM-dd"), doctorId) == false)
                {
                    LableUpdate.Visible = true;
                    ChkTime.Visible = true;
                    UpdateSubmit.Visible = true;
                    ddTimeInser1.Visible = true;
                    ddTimeInser2.Visible = true;
                    ddTimeInser3.Visible = true;
                    ddTimeInser4.Visible = true;
                    ddTimeInser5.Visible = true;
                    ddTimeInser6.Visible = true;
                    Session["update"] = "1";
                    if (time.isExitDate(Calendar.SelectedDate.ToString("yyyy-MM-dd"), doctorId) == false)
                    {
                        for (int j = 0; j < gvTime.Rows.Count; j++)
                        {
                            foreach (ListItem listItem in ChkTime.Items)
                            {
                                if (listItem.Value == gvTime.Rows[j].Cells[1].Text)
                                    listItem.Selected = true;
                            }
                        }
                    }
                }
                else lblMassage1.Text = Calendar.SelectedDate.ToString("yyyy-MM-dd") + "没有数据";
            }
        }

        protected void deleteTime_Click(object sender, EventArgs e)
        {
            Session.Remove("insert");
            Session.Remove("update");
            lblMassage1.Text = "";
            lblMassage.Text = "";
            if (Request.Form["myRadio"] == null)
            {
                lblMassage1.Text = "没有选中项";
                jsLiteral.Text = "";
            }
            else
            {
                string value;
                value = Request.Form["myRadio"].ToString();
                jsLiteral.Text = "setNowRadio('" + value + "');";
                time.deleteTime(gvTime.Rows[Convert.ToInt16(value)].Cells[0].Text);
                lblMassage.Text = gvTime.Rows[Convert.ToInt16(value)].Cells[0].Text + "号记录删除成功";
                gvTime.DataSourceID = "ldsTime";
                gvTime.DataBind();
            }
        }
        protected void insertAllTime_Click(object sender, EventArgs e)
        {
            Session.Remove("update");
            LableInsert.Visible = true;
            ChkTime.Visible = true;
            InsertSubmit.Visible = true;
            ddTimeInser1.Visible = true;
            ddTimeInser2.Visible = true;
            ddTimeInser3.Visible = true;
            ddTimeInser4.Visible = true;
            ddTimeInser5.Visible = true;
            ddTimeInser6.Visible = true;
            Session["insert"] = "1";
        }
        protected void InsertSubmit_Click(object sender, EventArgs e)//增加一天的数据
        {
            LableInsert.Visible = true;
            ChkTime.Visible = true;
            InsertSubmit.Visible = true;
            ddTimeInser1.Visible = true;
            ddTimeInser2.Visible = true;
            ddTimeInser3.Visible = true;
            ddTimeInser4.Visible = true;
            ddTimeInser5.Visible = true;
            ddTimeInser6.Visible = true;
            string doctorId;
            if (Calendar.SelectedDate.ToString("yyyy-MM-dd") == "0001-01-01")
            {
                lblMassage.Text = "请选择日期";
            }
            else if (Session["time"] == null)
            {
                lblMassage.Text = "请选择时间";
            }
            else
            {
                int i = gvDoctor.SelectedIndex;//获得所选择的医生下表
                if (i != -1)//选择了医生
                {
                    //lblMassage.Text = i.ToString();
                    doctorId = gvDoctor.Rows[Convert.ToInt16(i)].Cells[0].Text;//得到医生ID
                    if (time.isExitDate(Calendar.SelectedDate.ToString("yyyy-MM-dd"), doctorId) == true)//判断该天数据是否存在
                    {
                        foreach (ListItem listItem in ChkTime.Items)
                        {
                            if (listItem.Selected)
                            {
                                if (listItem.Value == "8:30-9:30")
                                {
                                    time.insertTime(doctorId, listItem.Value, ddTimeInser1.SelectedValue, Calendar.SelectedDate);
                                }
                                else if (listItem.Value == "9:30-10:30")
                                {
                                    time.insertTime(doctorId, listItem.Value, ddTimeInser2.SelectedValue, Calendar.SelectedDate);
                                }
                                else if (listItem.Value == "10:30-11:30")
                                {
                                    time.insertTime(doctorId, listItem.Value, ddTimeInser3.SelectedValue, Calendar.SelectedDate);
                                }
                                else if (listItem.Value == "14:30-15:30")
                                {
                                    time.insertTime(doctorId, listItem.Value, ddTimeInser4.SelectedValue, Calendar.SelectedDate);
                                }
                                else if (listItem.Value == "15:30-16:30")
                                {
                                    time.insertTime(doctorId, listItem.Value, ddTimeInser5.SelectedValue, Calendar.SelectedDate);
                                }
                                else if (listItem.Value == "16:30-17:30")
                                {
                                    time.insertTime(doctorId, listItem.Value, ddTimeInser6.SelectedValue, Calendar.SelectedDate);
                                }
                            }
                        }
                        lblMassage.Text = "插入成功";
                    }
                    else lblMassage.Text = "已存在";
                    gvTime.DataSourceID = "ldsTime";
                    gvTime.DataBind();
                }
                else lblMassage.Text = "请选择医生";
            }
        }
        protected void UpdateSubmit_Click(object sender, EventArgs e)//修改一天对应的时间段以及人数
        {
            LableUpdate.Visible = true;
            ChkTime.Visible = true;
            UpdateSubmit.Visible = true;
            ddTimeInser1.Visible = true;
            ddTimeInser2.Visible = true;
            ddTimeInser3.Visible = true;
            ddTimeInser4.Visible = true;
            ddTimeInser5.Visible = true;
            ddTimeInser6.Visible = true;
            int i = gvDoctor.SelectedIndex;//获得所选择的医生下表
            if (i != -1)
            {
                string doctorId = gvDoctor.Rows[Convert.ToInt16(i)].Cells[0].Text;//得到医生ID
                time.deleteAllTime(Calendar.SelectedDate.ToString("yyyy-MM-dd"), doctorId);
                foreach (ListItem listItem in ChkTime.Items)
                {
                    if (listItem.Selected)
                    {
                        if (listItem.Value == "8:30-9:30")
                        {
                            time.insertTime(doctorId, listItem.Value, ddTimeInser1.SelectedValue, Calendar.SelectedDate);
                        }
                        else if (listItem.Value == "9:30-10:30")
                        {
                            time.insertTime(doctorId, listItem.Value, ddTimeInser2.SelectedValue, Calendar.SelectedDate);
                        }
                        else if (listItem.Value == "10:30-11:30")
                        {
                            time.insertTime(doctorId, listItem.Value, ddTimeInser3.SelectedValue, Calendar.SelectedDate);
                        }
                        else if (listItem.Value == "14:30-15:30")
                        {
                            time.insertTime(doctorId, listItem.Value, ddTimeInser4.SelectedValue, Calendar.SelectedDate);
                        }
                        else if (listItem.Value == "15:30-16:30")
                        {
                            time.insertTime(doctorId, listItem.Value, ddTimeInser5.SelectedValue, Calendar.SelectedDate);
                        }
                        else if (listItem.Value == "16:30-17:30")
                        {
                            time.insertTime(doctorId, listItem.Value, ddTimeInser6.SelectedValue, Calendar.SelectedDate);
                        }
                    }
                }
            }
            gvTime.DataSourceID = "ldsTime";
            gvTime.DataBind();
            lblMassage.Text = "修改成功";
        }

        protected void Calendar_SelectionChanged(object sender, EventArgs e)
        {
            if (Session["insert"] != null)
            {
                //lblMassage.Text = Calendar.SelectedDate.ToString("yyyy-MM-dd");
                LableInsert.Visible = true;
                ChkTime.Visible = true;
                InsertSubmit.Visible = true;
                ddTimeInser1.Visible = true;
                ddTimeInser2.Visible = true;
                ddTimeInser3.Visible = true;
                ddTimeInser4.Visible = true;
                ddTimeInser5.Visible = true;
                ddTimeInser6.Visible = true;
            }
        }

        protected void gvDoctor_SelectedIndexChanged(object sender, EventArgs e)
        {
            int page = gvDoctor.PageIndex;
            if (Session["indexTime"] != null && Session["pageTime"].ToString() == page.ToString())
            {
                gvDoctor.Rows[Convert.ToInt16(Session["indexTime"].ToString())].ForeColor = Color.Empty;
                gvDoctor.Rows[Convert.ToInt16(Session["indexTime"].ToString())].BackColor = Color.Empty;
            }
            int index = gvDoctor.SelectedIndex;//获得所选择的医生下标
            gvDoctor.Rows[Convert.ToInt16(index)].ForeColor = Color.Black;
            gvDoctor.Rows[Convert.ToInt16(index)].BackColor = ColorTranslator.FromHtml("#ccccd4");
            if (Session["insert"] != null)
            {
                LableInsert.Visible = true;
                ChkTime.Visible = true;
                InsertSubmit.Visible = true;
                ddTimeInser1.Visible = true;
                ddTimeInser2.Visible = true;
                ddTimeInser3.Visible = true;
                ddTimeInser4.Visible = true;
                ddTimeInser5.Visible = true;
                ddTimeInser6.Visible = true;
            }
            Session["indexTime"] = index.ToString();
            Session["pageTime"] = page.ToString();
        }

        protected void deleteAllDisease_Click(object sender, EventArgs e)
        {
            Session.Remove("insert");
            Session.Remove("update");
            int i = gvDoctor.SelectedIndex;//获得所选择的医生下标
            if (i == -1)
                lblMassage1.Text = "请选择医生";
            else if (Calendar.SelectedDate.ToString("yyyy-MM-dd") == "0001-01-01")
            {
                lblMassage1.Text = "请选择日期";
            }
            else
            {
                string doctorId = gvDoctor.Rows[Convert.ToInt16(i)].Cells[0].Text;//得到医生ID
                time.deleteAllTime(Calendar.SelectedDate.ToString("yyyy-MM-dd"), doctorId);
                lblMassage.Text = "删除成功";
                gvTime.DataSourceID = "ldsTime";
                gvTime.DataBind();
            }
        }

        protected void ChkTime_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (ListItem listItem in ChkTime.Items)
            {
                if (listItem.Selected)
                {
                    Session["time"] = Session["time"] + listItem.Value + ",";
                }
            }
        }

    }
}